import { webview } from '@/auto-server/common/webview';

export default function() {
    webview.on("webloaded").subscribe(([_param, done]) => {
        requestMyScreenCapture(done);
    })
    
    // todo使用core包的获取状态栏高度
    webview.on("getStatusBarHeight").subscribe(([_param, done]) => {
        var resources = context.getResources();
        var resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        var statusBarHeight = resources.getDimensionPixelSize(resourceId);
        var density = context.getResources().getDisplayMetrics().density;
        done(parseInt(statusBarHeight / density));
    })
    
    webview.on("toast").subscribe(([string, done]) => {
        done();
        toastLog(string);
    });
    webview.on("exit").subscribe(([_param, done]) => {
        done();
        exit();
    });
    // 获取设置清单
    webview.on('getSettings').subscribe(([_param, done]) => {
        let ret = [{
                desc: '无障碍服务',
                name: 'autoService',
                type: 'autojs_inner_setting_auto_service',
                enabled: !!auto.service
            },
            {
                desc: '悬浮窗权限',
                name: 'floatyPerminssion',
                type: 'autojs_inner_setting_floaty_permission',
                enabled: floaty.checkPermission()
            }, {
                desc: '音量上键停脚本及程序',
                name: 'stop_all_on_volume_up',
                type: 'autojs_inner_setting',
                enabled: $settings.isEnabled('stop_all_on_volume_up')
            }, {
                desc: '前台服务',
                name: 'foreground_service',
                type: 'autojs_inner_setting',
                enabled: $settings.isEnabled('foreground_service')
            },
            {
                desc: '截图权限',
                name: 'screenCapturePermission',
                type: 'autojs_inner_settings_capture_permission',
                enabled: !!images.getScreenCaptureOptions()
            },
            {
                desc: '忽略电池优化',
                name: 'ignoreBatteryOptimization',
                type: 'autojs_inner_setting_power_manage',
                enabled: $power_manager.isIgnoringBatteryOptimizations()
            },
        ];
        done(ret);
    });
    webview.on('saveSetting').subscribe(([item, done]) => {
        if ('autojs_inner_setting' === item.type) {
            $settings.setEnabled(item.name, item.enabled);
            done(true);
        } else if ('autojs_inner_setting_power_manage' === item.type) { // 忽略电池优化
            if (item.enabled) {
                $power_manager.requestIgnoreBatteryOptimizations();
                let count = 0;
                let timmer = setInterval(function () {
                    count++;
                    if (count > 10) {
                        clearInterval(timmer);
                        done(false);
                    }
                    let newStatus = $power_manager.isIgnoringBatteryOptimizations();
                    if (newStatus) {
                        clearInterval(timmer);
                        done(true);
                    }
                }, 1000)
            } else {
                done(false);
            }
        } else if ('autojs_inner_setting_auto_service' === item.type) { // 无障碍
            if (item.enabled) {
                // 启用无障碍服务
                if (isRoot) {
                    $settings.setEnabled('enable_accessibility_service_by_root', true);
                } else {
                    $settings.setEnabled('enable_accessibility_service_by_root', false);
                    toastLog('在回到程序前请手动开启无障碍服务');
                }
                threads.start(function () {
                    auto.waitFor();
                    done(true);
                });
            } else {
                auto.service && auto.service.disableSelf();
                done(true);
            }
        } else if ('autojs_inner_setting_floaty_permission' === item.type) {
            if (item.enabled) {
                toastLog('在回到程序前请手动开启悬浮窗权限');
                floaty.requestPermission();
                let count = 0;
                let timmer = setInterval(function () {
                    count++;
                    if (count > 20) {
                        clearInterval(timmer);
                        done(false);
                    }
                    if (floaty.checkPermission()) {
                        clearInterval(timmer);
                        done(true);
                    }
                }, 1000)
            } else {
                // 开启后不允许取消
                done(false);
            }
        } else if ('autojs_inner_settings_capture_permission' === item.type) {
            if (item.enabled) {
                threads.start(function () {
                    done(!!images.requestScreenCapture());
                });
            } else {
                images.stopScreenCapture();
                done(true);
            }
        }
    });
}
